<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\exception\ValidateException;

/***
 * Class Borrow 借阅
 * @package app\admin\controller
 */
class Borrow extends Base
{
    /**
     * index #借阅列表
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/11/25
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $page = input('page', 1);
        $pageSize = input('pageSize', 15);
        //$bTitle = input('keyword');
        $userId = input('user_id');

        $where = [];
        //$bTitle && $where['b_title'] = $bTitle;
        $userId && $where['user_id'] = $userId;
        $list = \app\common\model\Borrow::with(['book', 'user'])->where($where)->paginate($pageSize);
        $this->success($list);
    }

    /**
     * findBorrowList #查找借阅列表
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/11/25
     * @throws \think\db\exception\DbException
     */
    public function findBorrowList()
    {
        $userId = input('user_id');
        $userId || $this->error('参数错误');

        $where = ['status' => \app\common\model\Borrow::STATUS_0];
        $userId && $where['user_id'] = $userId;
        $list = \app\common\model\Borrow::with(['book'])->where($where)->select();
        $this->success($list);
    }

    /**
     * add #添加借阅记录
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/11/25
     */
    public function add()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            $params['user_type'] = \app\common\model\User::USER_TYPE_10;
            $params['operate_user_type'] = $this->auth->user_type;
            $params['operate_user_id'] = $this->auth->id;
            validate(\app\common\validate\Borrow::class)->check($params);

            //查询借书人的信息
            $jsUser = \app\common\model\User::find($params['user_id']);
            $jsUser->isEmpty() && $this->error('借阅用户的信息不存在');
            $params['user_type'] = $jsUser->user_type;

            //是否已经借阅
            $isJy = \app\common\model\Borrow::where(['status' => \app\common\model\Borrow::STATUS_0, 'book_id' => $params['book_id'], 'user_type' => $params['user_type'], 'user_id' => $params['user_id']])->count();
            $isJy && $this->error('您已借阅，不可操作');

            //保存数据
            $res = (new \app\common\model\Borrow())->save($params);
            $res === false && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * updateStatus #更新借阅状态
     * api post /{controller}/{method}
     *
     * @auth kouweihong
     * @date 2020/11/25
     */
    public function updateStatus()
    {
        $this->request->isPost() || $this->error('非法操作');
        $params = input('post.');
        $params || $this->error('提交参数为空');

        try {
            validate(\app\common\validate\Borrow::class)->scene('updateStatus')->check($params);

            //查询书籍信息
            $book = \app\common\model\Book::find($params['book_id']);
            $book || $this->error('书籍信息不存在');

            //查询借书人的信息
            $user = \app\common\model\User::find($params['user_id']);
            $user || $this->error('用户信息不存在');

            //保存数据
            $res = \app\common\model\Borrow::where(['book_id' => $params['book_id'], 'user_id' => $params['user_id']])->save(['status' => \app\common\model\Borrow::STATUS_2]);
            $res === false && $this->error('操作失败');

            $this->success();
        } catch (ValidateException $e) {
            $this->error($e->getMessage());
        }
    }
}
